import winston from 'winston';
import 'winston-daily-rotate-file';
import config from 'config';

const dailyFileTransport = new winston.transports.DailyRotateFile({
  json: false,
  filename: 'agent-%DATE%.log',
  dirname: 'logs',
  datePattern: 'YYYY-MM-DD',
  zippedArchive: true,
  maxSize: '20m',
  maxFiles: '7d',
  level: config.get('logger.file.level')
});

const { combine, splat, timestamp, simple, printf, align } = winston.format;

const logFormat = combine(align(), splat(), timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), simple(), printf(
  info => `${info.timestamp} [${info.level}] ${info.message}`
));

const options: winston.LoggerOptions = {
  format: logFormat,
  transports: [
    new winston.transports.Console({
      level: config.get('logger.console.level')
    }),
    dailyFileTransport
  ]
};

export enum LogLevel {
  Debug,
  Info,
  Warn,
  Error
}

export const logger = winston.createLogger(options);
logger.info('Logging service initialized');